package org.zfes.snowier.cloud.zbss.layer.tc.biz.service.impl;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.encypt.MD5Util;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.idcenter.IDGenerator;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.ILAuthUserService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.dao.LAuthUserMapper;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.LAuthUser;

@Service
public class LAuthUserServiceImpl  implements ILAuthUserService{
 	@Autowired
	private LAuthUserMapper authUserMapper;
//	public static void main(String[] args) {
//		String salt=UUID.randomUUID().toString();
//		String psd="adminranran";
//		String pass=MD5Util.encrypt(psd, salt);
//		System.out.println(salt);
//		System.out.println(pass);
//	}
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public Long createAuthUser(LAuthUser authUser) {
		
		String salt=UUID.randomUUID().toString();
		authUser.setSalt(salt);
		String pass=MD5Util.encrypt(authUser.getPassword(), salt);
		authUser.setPassword(pass);
		ZBeanUtil.validateBean(authUser);
		
		LAuthUser user=authUserMapper.selectLAuthUserByAccount(authUser.getAccount());
		if( user!=null){
			ZAlert.Error("账号已经存在");
		}
		authUserMapper.insertSelective(authUser);
		
		LAuthUser user23=authUserMapper.selectLAuthUserByAccount(authUser.getAccount());
		
		return user23.getId();
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setPwd(Long id,String  password) {
		LAuthUser authUser=authUserMapper.selectByPrimaryKey(id);
		ZAssert.notNull(authUser, "未查询到数据");
		
		String salt=UUID.randomUUID().toString();
		String pass=MD5Util.encrypt(password, salt);
		
		authUser.setPassword(pass);
		authUser.setSalt(salt);
		authUserMapper.updateByPrimaryKeySelective(authUser);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<LAuthUser> loadAuthUserById(Long id) {
		if(id!=null){
			return Optional.ofNullable(authUserMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAuthUser(LAuthUser authUser) {
		ZBeanUtil.validateBean(authUser);
		authUserMapper.updateByPrimaryKeySelective(authUser);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletAuthUser(Long id) {
		if(id!=null){
			authUserMapper.deleteByPrimaryKey(id);
			return;
		}
		ZAlert.Error("请选择要删除的数据");
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadAuthUserDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS(authUserMapper.selectListCount(pm), authUserMapper.selectList(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public LAuthUser selectByAccount(String account) {
		if(account!=null&&!account.isEmpty()){
			return authUserMapper.selectLAuthUserByAccount(account);
		}
		return null;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateUserName(Long id, String userName) {
		ZAssert.notNull(id, "id不能为空");
		ZAssert.hasText(userName, "姓名不能为空");
		authUserMapper.updateUserName(id, userName);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public int updateEnabled(Long id, int enabled) {
		ZAssert.notNull(id, "id不能为空");
		return authUserMapper.updateEnabled(id, enabled);
	}
	
}